package com.lgkj.utils;

import android.net.Uri;
import android.os.Environment;
import android.view.View;

import org.devio.takephoto.app.TakePhoto;
import org.devio.takephoto.compress.CompressConfig;
import org.devio.takephoto.model.CropOptions;
import org.devio.takephoto.model.LubanOptions;

import java.io.File;


/**
 * - 支持通过相机拍照获取图片
 * - 支持从相册选择图片
 * - 支持从文件选择图片
 * - 支持多图选择
 * - 支持批量图片裁切
 * - 支持批量图片压缩
 * - 支持对图片进行压缩
 * - 支持对图片进行裁剪
 * - 支持对裁剪及压缩参数自定义
 * - 提供自带裁剪工具(可选)
 * - 支持智能选取及裁剪异常处理
 * - 支持因拍照Activity被回收后的自动恢复
 * Author: crazycodeboy
 * Date: 2016/9/21 0007 20:10
 * Version:4.0.0
 * 技术博文：http://www.devio.org
 * GitHub:https://github.com/crazycodeboy
 * Email:crazycodeboy@gmail.com
 */
public class CustomHelper {

    private CustomHelper() {
        init();
    }

    private void init() {

    }

    public void onClick(View view, TakePhoto takePhoto) {
        File file = new File(Environment.getExternalStorageDirectory(), "/temp/" + System.currentTimeMillis() + ".jpg");
        if (!file.getParentFile().exists()) {
            file.getParentFile().mkdirs();
        }
        Uri imageUri = Uri.fromFile(file);
        takePhoto.onPickMultiple(1);
        takePhoto.onPickFromCaptureWithCrop(imageUri, getCropOptions(100,200));  //从文件

//        takePhoto.onPickFromGalleryWithCrop(imageUri, getCropOptions());//剪切
//        takePhoto.onPickFromCaptureWithCrop(imageUri, getCropOptions());  //剪切

        takePhoto.onPickFromDocumentsWithCrop(imageUri, getCropOptions(100,200));   //拍照
        configCompress(takePhoto, 100, 100);
        switch (view.getId()) {

        }
    }

    private void configCompress(TakePhoto takePhoto, int width, int height) {
        CompressConfig config;
        LubanOptions option = new LubanOptions.Builder().setMaxHeight(height).setMaxWidth(width).setMaxSize(102400).create();
        config = CompressConfig.ofLuban(option);
        config.enableReserveRaw(true);
        takePhoto.onEnableCompress(config, false);


    }

    private CropOptions getCropOptions(int height, int width) {

        CropOptions.Builder builder = new CropOptions.Builder();

        builder.setOutputX(width).setOutputY(height);
        builder.setWithOwnCrop(true);
        return builder.create();
    }

}
